package br.gendi.dialog;

import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

public class Visualizador implements IVisualizador {

	private Shell sShell = null;
	private TabFolder tabFolder = null;
	private Composite c = null;


	public Visualizador() {
		super();
		createSShell();
		//sShell.open();
	}

	private void createSShell() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.horizontalSpacing = 2;
		gridLayout.marginWidth = 2;
		gridLayout.marginHeight = 2;
		gridLayout.verticalSpacing = 2;
		sShell = new Shell();		   
Rectangle pDisplayBounds = sShell.getDisplay().getBounds();
		
        // Set Width and Height(doesn't matter value)
		int nMinWidth = pDisplayBounds.width  / 2;
		int nMinHeight = pDisplayBounds.height / 2;

         // This formulae calculate the shell's Left ant Top
		int nLeft = (pDisplayBounds.width - nMinWidth) / 2;
		int nTop = (pDisplayBounds.height - nMinHeight) / 2;
              
          // Set shell bounds,
		sShell.setBounds(nLeft, nTop, nMinWidth, nMinHeight);
		sShell.setText("Resultados");
		createTabFolder();
		sShell.setLayout(gridLayout);
		sShell.setSize(new Point(800,600));
		ImageData isLarge = br.gendi.rcp.Activator.getImageDescriptor("/icons/mainicon32.png").getImageData();
		ImageData isSmall = br.gendi.rcp.Activator.getImageDescriptor("/icons/mainicon16.png").getImageData();

		Image large = new Image(sShell.getDisplay(), isLarge);
		Image small = new Image(sShell.getDisplay(), isSmall);

		sShell.setImages(new Image[] {small, large});
	}

	private void createTabFolder() {
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessVerticalSpace = true;
		tabFolder = new TabFolder(sShell, SWT.NONE);
		tabFolder.setLayoutData(gridData);
		c = new Composite(new Shell(),SWT.NONE);
		c.setVisible(false);

	}

	public void adicionar( final String nome, final String html) {
		final Browser b = new Browser(tabFolder, SWT.NONE);
		b.setText(html);
		final TabItem tabItem1 = new TabItem(tabFolder, SWT.NONE);
		tabItem1.setControl(b);
		tabItem1.setText(nome);

		if (!sShell.isVisible())
			sShell.open();
		
	}
	
	
}
